package com.ruicar.afs.cloud.basic.proceeds.debit.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.basic.common.entity.BasicMainInfo;
import com.ruicar.afs.cloud.basic.common.service.BasicMainInfoService;
import com.ruicar.afs.cloud.basic.proceeds.debit.condition.DebitBatchCondition;
import com.ruicar.afs.cloud.basic.proceeds.debit.entity.BasicDebitBatch;
import com.ruicar.afs.cloud.basic.proceeds.debit.entity.BasicDebitBatchDetails;
import com.ruicar.afs.cloud.basic.proceeds.debit.entity.BasicDebitHistory;
import com.ruicar.afs.cloud.basic.proceeds.debit.entity.BasicDebitRecord;
import com.ruicar.afs.cloud.basic.proceeds.debit.service.BasicDebitBatchDetailsService;
import com.ruicar.afs.cloud.basic.proceeds.debit.service.BasicDebitBatchService;
import com.ruicar.afs.cloud.basic.proceeds.debit.service.BasicDebitHistoryService;
import com.ruicar.afs.cloud.basic.proceeds.debit.service.BasicDebitRecordService;
import com.ruicar.afs.cloud.basic.proceeds.debit.vo.DebitBatchDetailVo;
import com.ruicar.afs.cloud.basic.proceeds.debit.vo.DebitDetailRecordVo;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.util.EmptyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.function.Function;

/**
 * <p>Description: </p>
 *
 * @author ZC.GUO
 * @version 1.0
 * @date create on 2020-06-13 15:09
 */
@Slf4j
@RestController
@Api("扣款管理")
@RequestMapping("/debitDetail")
public class DebitDetailController {
    @Autowired
    private BasicDebitBatchDetailsService basicDebitBatchDetailsService;
    @Autowired
    private BasicDebitRecordService basicDebitRecordService;
    @Autowired
    private BasicDebitHistoryService basicDebitHistoryService;
    @Autowired
    private BasicMainInfoService basicMainInfoService;
    @Autowired
    private BasicDebitBatchService basicDebitBatchService;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    @PostMapping(value = "/queryDebitDetailList")
    @ApiOperation(value = "根据批次分页获取合同扣款记录")
    public IResponse<IPage<DebitBatchDetailVo>> queryDebitDetailList(@ModelAttribute DebitBatchCondition debitBatchCondition) {
        Page page = new Page(debitBatchCondition.getPageNumber(), debitBatchCondition.getPageSize());
        if (EmptyUtils.isNotEmpty(debitBatchCondition.getStartDate())){
            debitBatchCondition.setStartDate(DateUtil.beginOfDay(debitBatchCondition.getStartDate()));
        }
        if (EmptyUtils.isNotEmpty(debitBatchCondition.getEndDate())){
            debitBatchCondition.setEndDate(DateUtil.endOfDay(debitBatchCondition.getEndDate()));
        }
        IPage<BasicDebitBatchDetails> debitBatchDetailsIPage = basicDebitBatchDetailsService.page(page,Wrappers.<BasicDebitBatchDetails>query().lambda()
                .like(EmptyUtils.isNotEmpty(debitBatchCondition.getContractNo()),BasicDebitBatchDetails::getContractNo,debitBatchCondition.getContractNo())
                .eq(EmptyUtils.isNotEmpty(debitBatchCondition.getDeductBank()), BasicDebitBatchDetails::getDebitSignBank,debitBatchCondition.getDeductBank())
                .like(EmptyUtils.isNotEmpty(debitBatchCondition.getDebitAccountName()),BasicDebitBatchDetails::getDebitAccountName,debitBatchCondition.getDebitAccountName())
                .eq(EmptyUtils.isNotEmpty(debitBatchCondition.getContractStatus()),BasicDebitBatchDetails::getContractStatus,debitBatchCondition.getContractStatus())
                .eq(EmptyUtils.isNotEmpty(debitBatchCondition.getDebitStatus()),BasicDebitBatchDetails::getStatus,debitBatchCondition.getDebitStatus())
                .gt(EmptyUtils.isNotEmpty(debitBatchCondition.getStartDate()), BasicDebitBatchDetails::getCreateTime,debitBatchCondition.getStartDate())
                .lt(EmptyUtils.isNotEmpty(debitBatchCondition.getEndDate()),BasicDebitBatchDetails::getCreateTime,debitBatchCondition.getEndDate())
                .orderByAsc(BasicDebitBatchDetails::getContractNo)
        );
        return IResponse.success(debitBatchDetailsIPage.convert(new Function<BasicDebitBatchDetails, DebitBatchDetailVo>() {

            @Override
            public DebitBatchDetailVo apply(BasicDebitBatchDetails debitBatchDetails) {

                DebitBatchDetailVo debitBatchDetailVo = new DebitBatchDetailVo();
                BeanUtils.copyProperties(debitBatchDetails,debitBatchDetailVo);


                BasicMainInfo mainInfo = basicMainInfoService.getOne(Wrappers.<BasicMainInfo>query().lambda()
                        .eq(BasicMainInfo::getContractNo,debitBatchDetails.getContractNo())
                );
                if (EmptyUtils.isNotEmpty(mainInfo)){
                    debitBatchDetailVo.setApplyNo(mainInfo.getApplyNo());
                }

                BasicDebitBatch debitBatch = basicDebitBatchService.getById(debitBatchDetails.getBatchId());
                if (EmptyUtils.isNotEmpty(debitBatch)){
                    debitBatchDetailVo.setBatchNo(debitBatch.getBatchNo());
                }


                List<BasicDebitRecord> debitRecordList = basicDebitRecordService.list(Wrappers.<BasicDebitRecord>query().lambda()
                        .eq(BasicDebitRecord::getBatchId,debitBatchDetails.getBatchId())
                        .eq(BasicDebitRecord::getContractNo,debitBatchDetails.getContractNo())
                );

                List<DebitDetailRecordVo> recordList = new ArrayList<>();
                if(CollectionUtils.isNotEmpty(debitRecordList)){
                    debitRecordList.forEach(basicDebitRecord -> {
                        DebitDetailRecordVo debitDetailRecordVo = new DebitDetailRecordVo();
                        BeanUtils.copyProperties(basicDebitRecord,debitDetailRecordVo);
                        recordList.add(debitDetailRecordVo);
                    });
                }else{
                    List<BasicDebitHistory> debitHistoryList = basicDebitHistoryService.list(Wrappers.<BasicDebitHistory>query().lambda()
                            .eq(BasicDebitHistory::getBatchId,debitBatchDetails.getBatchId())
                            .eq(BasicDebitHistory::getContractNo,debitBatchDetails.getContractNo())
                    );
                    if(CollectionUtils.isNotEmpty(debitHistoryList)){
                        debitHistoryList.forEach(basicDebitHistory -> {
                            DebitDetailRecordVo debitDetailRecordVo = new DebitDetailRecordVo();
                            BeanUtils.copyProperties(basicDebitHistory,debitDetailRecordVo);
                            debitDetailRecordVo.setGoingAmt(basicDebitHistory.getDebitAmt());
                            recordList.add(debitDetailRecordVo);
                        });
                    }
                }
                debitBatchDetailVo.setRecordList(recordList);
                return debitBatchDetailVo;
            }
        }));
    }
}
